<?php

/**
 * +---------------------------------------------------------------
 * | Author: 北京永青宇创科技有限公司
 * | Date: 2020/03/12
 * | 版本: 6.0
 * +---------------------------------------------------------------
 * | CopyRight: 北京永青宇创科技有限公司 All rights reserved.
 * +---------------------------------------------------------------
 * | 版权声明：云豹不是一个自由软件，是云豹官方推出的商业源码。
 * | 严禁在未经许可的情况下拷贝、复制、传播、使用云豹的任意代码
 * | 如有违反，请立即删除，否则您将面临承担相应，法律责任的风险。
 * | 如果需要取得官方授权，请联系官方 https://www.gepardshop.com
 * +---------------------------------------------------------------
 */

namespace app\mall\controller;

class MallOrder extends MallHome {

	public function CreateOrder($id = 0)
	{
		$goods_id = input('id');
		if( ! $goods_id) {
			$this->error('商品id错误');
		}
		$info = model('mall/mallGoods')->find($goods_id);
		if( ! $info) {
			$this->error('商品不存在');
		}
		model('mall/mallOrder')->out_pay_sn = \yunbao\order::createOutTradeNo();
		model('mall/mallOrder')->goods_id   = $info->id;
		model('mall/mallOrder')->buy_num    = 1;
		model('mall/mallOrder')->uid        = $this->auth->id;
		model('mall/mallOrder')->price      = sprintf("%.2f", $info->price * 1);
		model('mall/mallOrder')->pay_status = 1;

		model('mall/mallOrder')->save();
		if(model('mall/mallOrder')->id) {
			$pay_data['out_trade_no'] = model('mall/mallOrder')->out_pay_sn;
			$pay_data['title']        = '购买-' . $info->title;
			$pay_data['money']        = model('mall/mallOrder')->price / 10;
			$pay_data['callback']     = 'model:Mall/MallOrder:callback';
			$pay_data['return_url']   = url('mall/mallOrder/notify', '', true, true);
			$pay_data['allow_redbag'] = true;
			$pay_data['allow_coupon'] = true;
			$pay_data['allow_third']  = true;
			$pay_data['allow_money']  = true;
			$pay_data['status']       = 1;
			session('pay_data_' . $pay_data['out_trade_no'], $pay_data);
			$this->redirect(url('paycenter/gateway/create', ['out_trade_no' => $pay_data['out_trade_no']], true, true)); // 余额支付接口
		} else {
			$this->error('订单错误' . model('mall/mallOrder')->getError());
		}
	}

	public function notify()
	{
		$weixin_code = input('weixin_code');
		if(empty($weixin_code)) {
			$this->error('订单异常，如果你已支付，请联系管理员');
		}
		$cover_id = modC('PAY_CALLBACK', '', 'mall');
		if(empty($cover_id)) {
			$this->redirect(url('mall/index/index'));
		}
		$img = get_cover($cover_id);
		$this->assign('img', $img);
		$this->assign('msg', '支付成功');
		$this->assign('wait', 10);
		$this->assign('url', url('mall/index/index'));
		$this->assign('weixin_code', $weixin_code);

		return $this->fetch('mallorder/notify');
	}
}